package com.ruoyi.service.jpjs.bi_importantmatter.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.Ztree;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.mapper.jpjs.bi_importantmatter.ImportantMatterMapper;
import com.ruoyi.service.jpjs.bi_importantmatter.ImportantMatterService;
import com.ruoyi.util.ExcelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class ImportantMatterServiceImpl implements ImportantMatterService {

    @Autowired
    private ImportantMatterMapper importantMatterMapper;

    @Override
    public List<Map<String, Object>> selectImportantMatterList(Map<String, Object> map) {
        return importantMatterMapper.selectImportantMatterList(map);
    }

    @Override
    public Map<String, Object> selectImportantMatter(String id) {
        return importantMatterMapper.selectImportantMatter(id);
    }

    @Override
    public int addImportantMatter(Map<String, Object> map) {
        return importantMatterMapper.addImportantMatter(map);
    }

    @Override
    public int editImportantMatter(Map<String, Object> map) {
        return importantMatterMapper.editImportantMatter(map);
    }

    @Override
    public int deleteImportantMatter(String id) {
        return importantMatterMapper.deleteImportantMatter(id);
    }

    @Override
    public List<Ztree> getProTree() {
        List<Map<String, Object>> list = importantMatterMapper.getProTree(ShiroUtils.getSysUser().getUserId());
        return initZtree(list);
    }

    @Override
    public List<Map<String, Object>> getMonths() {
        return importantMatterMapper.getMonths();
    }

    @Override
    public List<Map<String, Object>> getYears() {
        return importantMatterMapper.getYears();
    }

    @Override
    public List<Map<String, Object>> getWeeks() {
        return importantMatterMapper.getWeeks();
    }

    @Override
    public List<Map<String, Object>> getBuild(String proId) {
        return importantMatterMapper.getBuild(proId);
    }

    @Override
    public Map<String, Object> getProject(String proId) {
        return importantMatterMapper.getProject(proId);
    }

    @Override
    public Map<String, Object> getCompany(String companyId) {
        return importantMatterMapper.getCompany(companyId);
    }

    private List<Ztree> initZtree(List<Map<String, Object>> projectList) {
        List<Ztree> ztrees = new ArrayList();
        for (Map<String, Object> map : projectList) {
            Ztree ztree = new Ztree();
            ztree.setId((String) map.get("id"));
            ztree.setpId((String) map.get("parentId"));
            ztree.setName((String) map.get("name"));
            ztree.setTitle((String) map.get("name"));
            ztree.setGrade((String) map.get("grade"));
            ztrees.add(ztree);
        }
        return ztrees;
    }

    @Override
    public int importData(MultipartFile file) {
        String[] colNames = new String[]{"compName", "proName", "matter", "dutyBy", "planTime", "superviseDynamic", "realityTime", "matterSource"};
        List<Map<String, Object>> list = ExcelUtils.getExcelData(file, colNames);
        if (list.size() > 0) {
            int endIndex = 100;
            for (int i = 0; i < list.size(); i = endIndex + 1, endIndex += 100) {
                importantMatterMapper.addImportImportantMatter(list.subList(i, endIndex >= list.size() ? list.size() : endIndex + 1));
            }
        }
        return 1;
    }

    @Override
    public AjaxResult importTemplate() {
        List<String> colNameList = new ArrayList<String>();
        colNameList.add("工程公司");
        colNameList.add("项目名称");
        colNameList.add("事项");
        colNameList.add("责任人");
        colNameList.add("要求完成时间");
        colNameList.add("督办情况");
        colNameList.add("更新时间");
        colNameList.add("事项来源");
        ExcelUtils.exportData(colNameList, new ArrayList<Map<String, Object>>(), "重要事项模板");
        return AjaxResult.success("重要事项模板.xls");
    }

    @Override
    public List<Map<String, Object>> getProjectList(String compId) {
        return importantMatterMapper.getProjectList(compId);
    }

    @Override
    public List<Map<String, Object>> getCompanyList() {
        return importantMatterMapper.getCompanyList();
    }
}
